package com.hzncc.scaffold.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Bo.RecordListBo;
import com.hzncc.scaffold.entity.Other.Oneplusxrecord;
import com.hzncc.scaffold.entity.Other.Oneplusxtraining;
import com.hzncc.scaffold.entity.Other.User;
import com.hzncc.scaffold.entity.Vo.CertificateVos;
import com.hzncc.scaffold.entity.Vo.CoordinateVo;
import com.hzncc.scaffold.entity.Vo.OnePlusXRecordListVo;
import com.hzncc.scaffold.mapper.OneplusxrecordMapper;
import com.hzncc.scaffold.service.OneplusxrecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 1+X考试记录表 服务实现类
 * </p>
 *
 * @author DrunkBear
 * @since 2022-04-27
 */
@Service
public class OneplusxrecordServiceImpl extends ServiceImpl<OneplusxrecordMapper, Oneplusxrecord> implements OneplusxrecordService {

    @Autowired
    OneplusxrecordMapper oneplusxrecordMapper;

    @Override
    public List<OnePlusXRecordListVo> recordList(String activityId, Integer type) {
        ArrayList<OnePlusXRecordListVo> onePlusXRecordListVos = new ArrayList<>();
        QueryWrapper<RecordListBo> qw = new QueryWrapper<>();

//        if(activityId!=null){
//            qw.eq("oneplusxrecord.activityId",activityId).or().eq("belongTo",type).isNull("belongTo");
//        }


        List<RecordListBo> recordListBos = oneplusxrecordMapper.oneplusxrecordRecordList(activityId,qw);

        for (RecordListBo recordListBo : recordListBos) {
            OnePlusXRecordListVo onePlusXRecordListVo = BeanUtil.copyProperties(recordListBo, OnePlusXRecordListVo.class,"coordinate");
            CertificateVos certificate = new CertificateVos();
            if(recordListBo.getCertificateId()!=null){
                certificate.setId(recordListBo.getCertificateId());
            }
            if(StrUtil.isNotEmpty(recordListBo.getCertNum())){
                certificate.setCertNum(recordListBo.getCertNum());
            }
            if(StrUtil.isNotEmpty(recordListBo.getImgUrl())){
                certificate.setImgUrl(recordListBo.getImgUrl());
            }


            CoordinateVo coordinateVo = new CoordinateVo();
            if(StrUtil.isNotEmpty(recordListBo.getCoordinate())){
                String coordinate = recordListBo.getCoordinate();
                coordinateVo = JSONUtil.toBean(coordinate, CoordinateVo.class);
            }

            User owner = new User();
            owner.setId(recordListBo.getUserId());
            owner.setAccount(recordListBo.getAccount());
            owner.setGender(recordListBo.getGender());
            owner.setIdCardNum(recordListBo.getIdCardNum());
            owner.setInstituteName(recordListBo.getInstituteName());
            owner.setName(recordListBo.getUserName());
            owner.setRealname(recordListBo.getRealname());

            Oneplusxtraining oneplusxtraining = new Oneplusxtraining();
            oneplusxtraining.setId(recordListBo.getOneplusxtrainingId());
            oneplusxtraining.setActivityStart(recordListBo.getActivityStart());
            oneplusxtraining.setActivityEnd(recordListBo.getActivityEnd());
            oneplusxtraining.setCertTemplate(recordListBo.getCertTemplate());
            oneplusxtraining.setCoordinate(recordListBo.getCoordinate());
            oneplusxtraining.setSetHeight(recordListBo.getSetHeight());
            oneplusxtraining.setSetWidth(recordListBo.getSetWidth());
            oneplusxtraining.setTitle(recordListBo.getTitle());

            onePlusXRecordListVo.setOwner(owner);
            onePlusXRecordListVo.setCoordinate(coordinateVo);
            onePlusXRecordListVo.setCertificate(certificate);
            onePlusXRecordListVo.setOneplusxtraining(oneplusxtraining);
            onePlusXRecordListVos.add(onePlusXRecordListVo);
        }

        return onePlusXRecordListVos;
    }
}
